package com.qujia.station.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.Oil;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.StationOillGun;
import com.qujia.station.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 油站信息Controller
 *
 * @author qujia
 * @date 2020-12-08
 */
@Api("油品信息接口")
@RestController
@RequestMapping("/oil/info")
@Slf4j
public class OilController extends BaseController
{
    @Autowired
    private IOilService oilService;
    @Autowired
    private IStationOillGunService stationOilGunService;
    @Autowired
    private IStationService stationService;

    @Autowired
    private MemberManagementService memberManagementService;

    @Autowired
    private TeamManagementService teamManagementService;
    /**
     * 查询油站信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Oil oil)
    {
        startPage();
        oil.setDeleteStatus("0");
        List<Oil> oils = oilService.selectOilList(oil);
        return getDataTable(oils);
    }
    @ApiOperation("通过登录用户获取油企的油品信息")
    @ApiImplicitParam(name = "listByLogin")
    @GetMapping("/listByLogin")
    public TableDataInfo listByStationId()
    {
        LoginUser current=SecurityUtils.getLoginUser();
        Station station=new Station();

        //获取等级为五或六的用户油品信息
        List<Oil> oilInfoList = getOilInfoList(current);
        if (CollectionUtil.isNotEmpty(oilInfoList)){
            return getDataTable(oilInfoList);
        }


        station.setEnterpriseId(current.getUser().getEnterpriseId());
        List<Station> stations=stationService.selectStationList(station);
        List<Oil> oils=new ArrayList<>();
        List<Long> oilIds=new ArrayList<>();
        for(Station station1:stations){
            StationOillGun stationOillGun=new StationOillGun();
            stationOillGun.setStationId(station1.getStationId());
            stationOillGun.setDeleteStatus("0");
            List<StationOillGun> stationOillGunList=stationOilGunService.selectStationOillGunList(stationOillGun);
            for(StationOillGun stationOillGun1:stationOillGunList){
                oilIds.add(stationOillGun1.getOilId());
            }

        }
        //先进行去重，在进行查询
        List<Long> oilIdlist = oilIds.stream().distinct().collect(Collectors.toList());
        for(Long id:oilIdlist){
            Oil oil=oilService.selectOilById(id);
            oils.add(oil);
        }
        return getDataTable(oils);
    }

    @ApiOperation("通过油企id获取油企的油品信息")
    @ApiImplicitParam(name = "listByStationId")
    @GetMapping("/listByStationId")
    public TableDataInfo listoidByStationId(Long stationId)
    {
        //LoginUser current=SecurityUtils.getLoginUser();
        //Station station=new Station();
        //station.setEnterpriseId(current.getUser().getEnterpriseId());
        //List<Station> stations=stationService.selectStationList(station);
        List<Oil> oils=new ArrayList<>();
        List<Long> oilIds=new ArrayList<>();
        StationOillGun stationOillGun=new StationOillGun();
        stationOillGun.setStationId(stationId);
        stationOillGun.setDeleteStatus("0");
//        for(Station station1:stations){
            List<StationOillGun> stationOillGunList=stationOilGunService.selectStationOillGunList(stationOillGun);
            for(StationOillGun stationOillGun1:stationOillGunList){
                oilIds.add(stationOillGun1.getOilId());
            }

//        }
        //先进行去重，在进行查询
        List<Long> oilIdlist = oilIds.stream().distinct().collect(Collectors.toList());
        for(Long id:oilIdlist){
            Oil oil=oilService.selectOilById(id);
            oils.add(oil);
        }
        return getDataTable(oils);
    }

    /**
     * 新增油品
     */
    @Log(title = "增加油品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Oil oil)
    {

        return toAjax(oilService.insertOil(oil));
    }

    /**
     * 逻辑删除油品信息
     */
    @PostMapping(value = "/delteOil")
    public AjaxResult deleteOil(@RequestBody Oil oil)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Oil oil1=new Oil();
        oil1.setOilId(oil.getOilId());
        oil1.setDeleteStatus("1");
        oil1.setDeleteUser(currentUser.getUser().getUserId());
        oil1.setDeleteDate(new Date());
        return toAjax(oilService.updateOil(oil1));
    }


    /**
     * 获取油站信息详细信息
     */
    @GetMapping(value = "/{oilId}")
    public AjaxResult getInfo(@PathVariable("oilId") Long oilId)
    {
        Oil oil=oilService.selectOilById(oilId);

        return AjaxResult.success(oil);
    }

    /**
     * 修改油站信息
     */
    @Log(title = "油站信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Oil oil)
    {

        return toAjax(oilService.updateOil(oil));
    }

    @ApiOperation(value = "根据油企账号获取所有油品", notes = "根据油企账号获取所有油品")
    @GetMapping("/getListByEnterprise")
    public AjaxResult getListByEnterprise() {
        return AjaxResult.success(oilService.getListByEnterprise());
    }


    @ApiOperation(value = "小程序根据油企或者油站获取所有油品", notes = "小程序根据油企账号获取所有油品")
    @GetMapping("/getListByEnterpriseOrStationId")
    public AjaxResult getListByEnterpriseOrStationId(@ApiParam(value = "油站id") @RequestParam(value = "stationId",required = false) Long stationId) {
        return AjaxResult.success(oilService.getListByEnterpriseOrStationId(stationId));
    }


    /**
     * 根据登录用户获取油品信息
     * @param current
     * @return
     */
    private  List<Oil> getOilInfoList(LoginUser current){
        Station station=new Station();
        if ("5".equals(current.getUser().getUserLevel())){
            //获取油企id
            log.info("获取该用户管理下的油企id：{}",current.getUser().getUserId());
            List<Long> managerEnterpriseId = teamManagementService.getManagerEnterpriseId(current.getUser().getUserId());
            List<Oil> oils=new ArrayList<>();
            List<Long> oilIds=new ArrayList<>();

            for (Long enterpriseId : managerEnterpriseId) {
                station.setEnterpriseId(enterpriseId);
                List<Station> stations=stationService.selectStationList(station);
                for(Station station1:stations){
                    StationOillGun stationOillGun=new StationOillGun();
                    stationOillGun.setStationId(station1.getStationId());
                    stationOillGun.setDeleteStatus("0");
                    List<StationOillGun> stationOillGunList=stationOilGunService.selectStationOillGunList(stationOillGun);
                    for(StationOillGun stationOillGun1:stationOillGunList){
                        oilIds.add(stationOillGun1.getOilId());
                    }
                }
            }
            //先进行去重，在进行查询
            List<Long> oilIdlist = oilIds.stream().distinct().collect(Collectors.toList());
            for(Long id:oilIdlist){
                Oil oil=oilService.selectOilById(id);
                oils.add(oil);
            }
            return oils;
        }
        if ("6".equals(current.getUser().getUserLevel())){
            //获取油企id
            log.info("获取该用户管理下的油企id：{}",current.getUser().getUserId());
            List<Long> memberEnterpriseId = memberManagementService.getMemberEnterpriseId(current.getUser().getUserId());
            List<Oil> oils=new ArrayList<>();
            List<Long> oilIds=new ArrayList<>();
            for (Long enterpriseId : memberEnterpriseId) {
                station.setEnterpriseId(enterpriseId);
                List<Station> stations=stationService.selectStationList(station);
                for(Station station1:stations){
                    StationOillGun stationOillGun=new StationOillGun();
                    stationOillGun.setStationId(station1.getStationId());
                    stationOillGun.setDeleteStatus("0");
                    List<StationOillGun> stationOillGunList=stationOilGunService.selectStationOillGunList(stationOillGun);
                    for(StationOillGun stationOillGun1:stationOillGunList){
                        oilIds.add(stationOillGun1.getOilId());
                    }
                }
            }
            //先进行去重，在进行查询
            List<Long> oilIdlist = oilIds.stream().distinct().collect(Collectors.toList());
            for(Long id:oilIdlist){
                Oil oil=oilService.selectOilById(id);
                oils.add(oil);
            }
            return oils;
        }
        return null;
    }
}
